{% extends 'admin.html' %}

{% block css %}
    <style>
        .error {
            color: red;
            position: absolute;

        }
    </style>
{% endblock %}

{% block left %}
    <ul class="layui-nav layui-nav-tree" lay-filter="test">
        <li class="layui-nav-item layui-nav-itemed ">
            <a class="" href="{% url 'admin_home' %}">书架</a>

        </li>
        <li class="layui-nav-item layui-nav-itemed">
            <a href="javascript:;">我的权限</a>
            <dl class="layui-nav-child">
                {% if request.admin == 'true' %}
                    <dd class="layui-this"><a href="{% url 'admin_toAdmin' %}">管理员管理</a></dd>
                {% endif %}
                <dd><a href="{% url 'manage_user' %}">用户管理</a></dd>
            </dl>
        </li>
        <li class="layui-nav-item">
            <a href="javascript:;">个人中心</a>
            <dl class="layui-nav-child">
                <dd><a href="{% url 'admin_detail' %}">账号详情</a></dd>

            </dl>
        </li>
    </ul>
{% endblock %}


{% block body %}
    <span class="layui-breadcrumb">
                <a href="{% url 'admin_home' %}">首页</a>
                <a><cite>我的权限</cite></a>
                <a href="{% url 'admin_toAdmin' %}">管理员管理</a>
            </span>
    <hr/>
    <h1 style="text-align: center">管理员列表</h1>
    <hr/>
    <button class="layui-btn layui-btn-checked" onclick="addAdmin()">添加管理员</button>
    <hr/>
    <table class="layui-table">
        <tr>
            <th>管理员</th>
            <th>性别</th>
            <th>年龄</th>
            <th>操作</th>
        </tr>
        {% for admin in admin_list %}
            <tr>
                <td>{{ admin.admin_name }}</td>
                {% if admin.sex is none %}
                    <td>待完善</td>
                {% else %}
                    {% if admin.sex == 1 %}
                        <td>男</td>
                    {% else %}
                        <td>女</td>
                    {% endif %}
                {% endif %}
                {% if admin.age is none %}
                    <td>待完善</td>
                {% else %}
                    <td>{{ admin.age }}</td>
                {% endif %}
                <td>
                    <button class="layui-btn layui-btn-danger" onclick="del({{ admin.id }})">删除</button>
                </td>
            </tr>
        {% endfor %}
    </table>




{% endblock %}

{% block js %}
    <script>
        function addAdmin() {
            layer.open({
                title: '添加管理员',
                content: '    <div class="layui-form-item">\n' +
                    '        <label class="layui-form-label">管理员名</label>\n' +
                    '        <div class="layui-input-block">\n' +
                    '            <input class="layui-input" type="text" id="adminName" placeholder="输入管理员名">\n' +
                    '            <p class="error" id="error"></p>\n' +
                    '        </div>\n' +
                    '    </div>',
                btn: ['添加', '取消'],
                yes: function (index, layero) {
                    if ($('#adminName').val() == "") {
                        $('#error').html('不能为空')
                        return
                    }

                    $.ajax({
                        url: '{% url 'add_admin' %}',
                        type: 'POST',
                        data: {'adminName': $('#adminName').val()},
                        dataType: 'JSON',
                        success: function (res) {
                            if (res.status) {
                                layer.open({
                                        content: '添加成功\n密码默认为admin+用户名',
                                        btn: ['确认'],
                                        yes: function (index, layero) {
                                            location.href = '{% url 'admin_toAdmin' %}'
                                        }
                                    }
                                )
                            } else {
                                $('#error').html('该管理员已存在')

                            }
                        }
                    })
                }
            })
        }

        function del(adminId) {
            layer.open({
                content: '确认删除？',
                btn: ['确认', '取消'],
                yes: function (index, layero) {
                    $.ajax({
                        url: '{% url 'del_admin' %}',
                        type: 'POST',
                        data: {'adminId': adminId},
                        dataType: 'JSON',
                        success: function (res) {
                            layer.open({
                                    content: '删除成功',
                                    btn: ['确认'],
                                    yes: function (index, layero) {
                                        location.href = '{% url 'admin_toAdmin' %}'
                                    }
                                }
                            )
                        }
                    })
                }
            })
        }
    </script>
{% endblock %}